﻿#include "VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL.h"

#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKPhyscisPropertyGeneralLaminateIsoLayer.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include <QDebug>

namespace IO
{

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL::adaptR()
    {
        return readGeneralLaminateIsoLayer();
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL::adaptW()
    {
        return writeGeneralLaminateIsoLayer();
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL::readGeneralLaminateIsoLayer()
    {
        if (!_node)// 判断Fluid节点是否存在
        {
            return false;
        }

        bool b = true;
        //转换类型
        Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer* layerData = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer*>(_dataObj);
        QString dValue = readStrAttribute(_node, "damping_spectrum", &b);

        if(b)
        {
            int DLFSplectrum = getSpectraObjectIdByName(dValue);
            layerData->setDLFSplectrumID(DLFSplectrum);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read Spectra Error" + dValue);
        }

        //获取厚度
        double thicknessValue = readDoubleAttribute(_node, "thickness", &b);
        if(b)
        layerData->setThickness(thicknessValue);
        //获取材料名称
        QString materialValue = readStrAttribute(_node, "material", &b);

        if(b)
        {
            //根据材料名称获取材料id
            int materialId = getMaterialObjectIdByName(materialValue);
            layerData->setMaterialID(materialId);
        }
        else
        {
            //读材料读取异常
            consoleMessage(3,"Read Material Error" + materialValue);
        }
        //读取成功
        return b;
    }

    bool VAOneXMLAdaptorPhysicalPropertyGeneralLaminateIL::writeGeneralLaminateIsoLayer()
    {
        if (!_node)// 判断节点是否存在
        {
            return false;
        }

        //转换类型
        Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer* data = dynamic_cast<Acoustics::FITKPhyscisPropertyGeneralLaminateIsoLayer*>(_dataObj);

        if(!data)
        {
            return false;
        }

        if(!_node)
        {
            return false;
        }

        //写如属性值
        writeAttribute(_node, "material", getMaterialObjectNameById(data->getMaterialID()));
        writeAttribute(_node, "thickness", data->getThickness());
        writeAttribute(_node, "damping_spectrum", getSpectraObjectNameByID(data->getDLFSplectrumID()));
        return true;
    }
}
